/*
 Authors: ZhangXuelian
 	
 
 Changes:
 	
	
*/


#include "../libmain/gobal.h"

THREAD_CONTEXT g_mq_thread_context;
static void * mq_thread_function(void * arg);

#define MQ_THREAD_ALIVE_THRESHOLD				(60 * 100)
#define MQ_THREAD_PRIORITY						 4

bool create_mq_thread()
{
	bool ret;
	g_mq_thread_context.alive_threshold = MQ_THREAD_ALIVE_THRESHOLD;
	g_mq_thread_context.handler = mq_thread_function;
	g_mq_thread_context.priority = MQ_THREAD_PRIORITY;
	if (! (ret = create_thread(&g_mq_thread_context)))
		printf("%s: failed\n", __func__);
	return ret;
}

static void * mq_thread_function(void * arg)
{
	THREAD_CONTEXT * c = (THREAD_CONTEXT *)arg;
	if (! init_mq())
	{
		printf("init_mq failed\n");
		c->tick = 0;
		c->quited = true;
		return 0;
	}

	do_mq_main();

	deinit_mq();
	printf("cmd quited\n");
	c->quited = true;
	return NULL;
}

bool is_mq_thread_alive()
{
	if (! is_thread_alive(&g_mq_thread_context))
	{
		printf("%s: mq failed %u %u\n", __func__, g_mq_thread_context.tick, get_tickcount());
		return false;
	}
	return true;
}

